{extend name="layout:base" /}

{block name="content"}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="test-table-reload-btn" style="margin-bottom: 10px;">
                <div class="layui-inline">
                    <select name="kf_id" id="kf_id" lay-filter="kf_id">
                        <option value="0">请选择客服</option>
                        {volist name="kf_list" id="vo"}
                        <option value="{$vo.id}">{$vo.username}</option>
                        {/volist}
                    </select>
                </div>
                <div class="layui-inline">
                    <input class="layui-input" name="keywords" id="keywords" autocomplete="off">
                </div>
                <button class="layui-btn" data-type="reload">搜索</button>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="tbList" class="layui-table" lay-filter="tbList" ></table>
            <script type="text/html" id="op">
                {eq name="admin.id" value="1"}
                <input type="checkbox" name="state" value="{{d.id}}" lay-skin="switch" lay-text="非丢|丢单" lay-filter="state" {{ d.state== 2 ? 'checked' : '' }}>
                {/eq}

            </script>
        </div>
    </div>
</div>
{/block}

{block name="script"}
<script>
    layui.config({
        base: '__STATIC__/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'util','form'], function () {

        var $ = layui.$,form = layui.form,layer = layui.layer,table = layui.table;
        table.render({
            elem: '#tbList',
            url: "{:url('getList')}?type=stop",
            cellMinWidth: 80,
            cols: [[
                {field: 'id', fixed: 'left',width:60, title: 'id', sort: false},
                {field: 'name',fixed:'left',title:'用户名',sort: true},
                {field: 'mobile',fixed:'left',title:'手机',sort: true},
                {field: 'title',title:'公司名称',sort: true},
                {field: 'card',title:'银行卡号',sort: true},
                {field: 'created_time',title:'开户时间'},
                {field: 'remarks',title: '备注',width:110},
                {field: 'kfname',title: '客服',width:76},
                {fixed: 'right', title: '操作', toolbar: '#op'}
            ]],
            page: true,
            limit:20
        });

        // 监听工具条
        table.on('tool(tbList)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定删除', {icon: 3, title: '提示'}, function (index) {
                    // obj.del();
                    // layer.close(index);
                    $.post("{:url('del')}", {id: data['id']}, function (result) {
                        if (result['code'] == 0) {
                            layer.msg(result['msg'], {icon: 1, time: 2000}, function () {
                                location.reload();
                            });
                        } else {
                            layer.msg(result['msg'], {icon: 3, time: 3000});
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                var url = "{:url('edit')}?id=" + data['id'];
                var index = layer.open({
                    type: 2,
                    title: '修改内容',
                    area: ['100%', '100%'],
                    shade: 0,
                    maxmin: true,
                    content: url,
                    zIndex: layer.zIndex
                });
            }
        });
        //监听单元格编辑
        table.on('edit(tbList)', function (obj) {
            var value = obj.value; //得到修改后的值
            var data = obj.data; //得到所在行所有键值
            var field = obj.field; //得到字段
            layer.msg('[ID: ' + data.id + '] ' + field + ' 字段更改为：' + value);

            // $.post("{:url('editSeat')}", {id: data.id, username: value}, function (result) {
            //     if (result['code'] == 0) {
            //         layer.msg(result['msg'], {icon: 1, time: 1000}, function () {
            //             location.reload();
            //         });
            //     } else {
            //         layer.msg(result['msg'], {icon: 3, time: 3000});
            //     }
            // });
        });

        form.on('switch(state)', function (obj) {
            var data = {id: this.value, 'state': obj.elem.checked ? 2 : 1};
            $.post("{:url('edit')}", data, function (result) {
                if (result['code'] == 0) {
                    layer.msg('操作成功');
                    location.reload();
                } else {
                    layer.msg(result['msg'], {icon: 3, time: 3000});
                }
            });
        });
        form.on('switch(istop)', function (obj) {
            var data = {id: this.value, 'istop': obj.elem.checked ? 1 : 0};
            $.post("{:url('edit')}", data, function (result) {
                if (result['code'] == 0) {
                    layer.msg('操作成功');
                    location.reload();
                } else {
                    layer.msg(result['msg'], {icon: 3, time: 3000});
                }
            });
        });


        //操作
        var $ = layui.$, active = {
            add: function () {
                var index = layer.open({
                    type: 2,
                    title: '添加内容',
                    area: ['100%', '100%'],
                    shade: 0,
                    maxmin: true,
                    content: "{:url('add')}",
                    zIndex: layer.zIndex
                });
            },
            reload: function(){
                var kf_id=$('#kf_id').val();
                var keywords=$('#keywords').val();

                //执行重载
                table.reload('tbList', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        kf_id: kf_id
                        ,keywords: keywords
                    }
                });

            }
        };

        $('.layui-card-header .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });

</script>
{/block}